﻿using Architecture.Repository.Interface;
using Architeture.Domain;
using Architeture.Domain.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Architecture.Domain.Service
{
    public class RoleMneuService : IRoleMneuService
    {
        protected readonly IRoleMenuRepsitory roleRepository;

        public RoleMneuService(IRoleMenuRepsitory roleRepository)
        {
            this.roleRepository = roleRepository;
        }



        public async Task<List<RoleMenuDTO>> GetRoleId(int id)
        {
            var newlist = (await roleRepository.GetAllAsync()).Where(x => x.RoleInfoid == id).Select(x => new RoleMenuDTO
            {
                MenuInfoid = x.MenuInfoid,
                RoleInfoid = x.RoleInfoid,
                RoleMenuid = x.RoleMenuid
            }).ToList();
            return newlist;
        }

        public async Task<int> RoleMenuAdd(int jid, List<int> mid)
        {
            var res = 0;
            foreach (var item in mid)
            {
                var newrole = (await roleRepository.GetAllAsync()).FirstOrDefault(x => x.MenuInfoid == item && x.RoleInfoid == jid);
                if (newrole != null)
                {
                    res = -1;
                }
                else
                {
                    res = 1;
                    var role = new RoleMenu();
                    role.MenuInfoid = item;
                    role.RoleInfoid = jid;
                    await roleRepository.RoleMenuAdd(role);
                }
            }
            return res;

        }
    }
}
